package com.server.chart;

import com.dao.chart.ChartsConfigMapper;
import com.dao.dataSource.DataConnectorMapper;
import com.dao.strategy.StrategysBdMapper;
import com.dao.strategy.StrategysChartMapper;
import com.dao.user.RecordMapper;
import com.model.vo.configVo.RecordInfo;
import com.util.GetTokenData;
import org.apache.ibatis.binding.BindingException;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import java.util.ArrayList;
import java.util.Iterator;
import java.util.List;
import java.util.Map;

@Component
public class ChartsUtilService {
    @Autowired
    GetTokenData getTokenData;
    @Autowired
    DataConnectorMapper dataConnectorMapper;
    @Autowired
    ChartsConfigMapper chartsConfigMapper;
    @Autowired
    StrategysBdMapper strategysBdMapper;
    @Autowired
    StrategysChartMapper strategysChartMapper;
    @Autowired
    RecordMapper recordMapper;

    public String getSqlStr(String token, int id, int dashId) {
        Map<String, String> resultMap = getTokenData.getTokenData(token);
        int userId = Integer.parseInt(resultMap.get("id"));
        int createID;
        int baseId;
        try {
            createID = chartsConfigMapper.getCreateId(id);
            baseId = dataConnectorMapper.getBaseId(id);
        }catch (BindingException exception){
            throw new BindingException();
        }
        String tableName = chartsConfigMapper.getTableName(id);
        System.out.println("table:"+tableName);
        System.out.println("userId:" + userId + ", createID:" + createID + "");
        if (dashId == 0) {
            if (userId == createID) {
                //如果数据源创建人ID跟用户ID一样，获得数据源全部权限
                if (tableName == null || "".equals(tableName)) {
                    return "";
                } else {
                    return columnNameUtil(userId, baseId, tableName, true, id, -1);
                }
            } else {
                //先拿图表创建者权限，在取分发权限
                System.out.println("你他妈的去哪里了");
                String sql = columnNameUtil(createID, baseId, tableName, true, id, -1);
                return columnNameUtil(userId, baseId, sql, false, id, -1);
            }
        }else {
            return columnNameUtil(createID, baseId, tableName, true, id, -1);
        }
    }

    public String columnNameUtil(int userId, int baseId, String tableName, boolean isOrder, int id, int createBId){
        //用户不是创建人
        List<String> strList = new ArrayList<>();

        if (isOrder){
            int baseCreateId;
            if (id != 0) {
                baseCreateId = chartsConfigMapper.getCreateIdBychart(id);
            }else {
                baseCreateId = createBId;
            }
            if (baseCreateId == userId ){
                System.out.println("等于吗");
                return "(" + tableName +")";
            }else {
                System.out.println("周到这里。。。。。。。。。。。。。。。。。");
                strList = strategysBdMapper.getSqlStr(userId, baseId);
                System.out.println("strList:" + strList);
            }
        }else {
            strList = strategysChartMapper.getSqlStr(userId, id);
        }

        if (strList.size() == 0){
            return "";
        }
        String sqlStr = "";
        Iterator isList = strList.iterator();
        while (isList.hasNext()){
            String str = (String) isList.next();
            if ("".equals(str) || str == null){
                return  "(select * from " + "(" + tableName +"))";
            }
            System.out.println("str111:" + str);
            StringBuilder sb = new StringBuilder(str);
            sqlStr = sqlStr + String.valueOf(sb .replace(1, 5, "( ")) + ") or";
        }
        System.out.println("sqlStr:" + sqlStr);
        StringBuilder sb = new StringBuilder(sqlStr);
        sqlStr = "(select * from " + "(" + tableName +")" + " where " + sb.replace(sb.length()-2,sb.length(), "") + ")";
        System.out.println("sql:" + sqlStr);
        return sqlStr;
    }

    /*
    浏览记录
     */
    public void inputRecord(String token, RecordInfo recordInfo){
        int tarId = recordInfo.getTarId();
        String type = recordInfo.getType();
        Map<String, String> resultMap = getTokenData.getTokenData(token);
        int userId = Integer.parseInt(resultMap.get("id"));
        int count = recordMapper.getCount(type, userId);


        List<Integer> integerList = recordMapper.getListId(type, userId);
        if (integerList.contains(tarId)){
            recordMapper.updateRecord(type, tarId);
        }else {
            if (count >= 10) {
                recordMapper.deleteRecord(type, userId);
            }
            recordMapper.inputRecord(type, userId, tarId);
        }
    }
}
